home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2000 Spring / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).7z / Oh!X 2000 Spring Special CD-ROM (Japan) (Part 2).bin / DXF / samples / multimedia / dplay / src / duel / dputil.cpp < prev    next >
C/C++ Source or Header  |  1999-07-23  |  13KB  |  426 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DPUtil.cpp
  3. //
  4. // Desc: Communication routines
  5. //
  6. // Copyright (C) 1995-1999 Microsoft Corporation. All Rights Reserved.
  7. //-----------------------------------------------------------------------------
  8. #include "duel.h"
  9. #include "DPUtil.h"
  10. #include "lobby.h"
  11.  
  12.  
  13. //-----------------------------------------------------------------------------
  14. // Globals
  15. //-----------------------------------------------------------------------------
  16. extern GUID           g_AppGUID;        // Duel's guid
  17. extern DPLCONNECTION* g_pDPLConnection;  // Connection settings
  18. extern BOOL           g_bUseProtocol;    // DirectPlay Protocol messaging
  19. extern BOOL           g_bAsyncSupported; // Asynchronous sends supported
  20.  
  21. DPSESSIONDESC2*       g_pdpsd;           // Durrent session description
  22. LPDIRECTPLAY4         g_pDP = NULL;      // DPlay object pointer
  23.  
  24.  
  25.                                      
  26.                                          
  27. //-----------------------------------------------------------------------------
  28. // Name: CheckCaps()
  29. // Desc: Helper function to check for certain Capabilities
  30. //-----------------------------------------------------------------------------
  31. VOID CheckCaps()
  32. {
  33.     HRESULT hr;
  34.     DPCAPS  dpcaps;
  35.     ZeroMemory( &dpcaps, sizeof(DPCAPS) );
  36.     dpcaps.dwSize = sizeof(DPCAPS);
  37.  
  38.     if( NULL == g_pDP )
  39.         return;
  40.     
  41.     // The caps we are checking do not differ for guaranteed msg
  42.     hr = g_pDP->GetCaps( &dpcaps, 0 );
  43.     if( FAILED(hr) )
  44.         return;
  45.  
  46.     // Determine if Aync messages are supported.
  47.     g_bAsyncSupported = (dpcaps.dwFlags & DPCAPS_ASYNCSUPPORTED) != 0;
  48.  
  49.     // Diagnostic traces of caps supported
  50.     if( g_bAsyncSupported )
  51.     {
  52.         TRACE(_T("Capabilities supported: Async %s %s %s\n"),
  53.                  (dpcaps.dwFlags & DPCAPS_SENDPRIORITYSUPPORTED ? _T("SendPriority") : _T("")),
  54.                  (dpcaps.dwFlags & DPCAPS_SENDTIMEOUTSUPPORTED ? _T("SendTimeout") : _T("")),
  55.                  (dpcaps.dwFlags & DPCAPS_ASYNCCANCELSUPPORTED
  56.                     ? _T("AsyncCancel") 
  57.                     : (dpcaps.dwFlags & DPCAPS_ASYNCCANCELALLSUPPORTED
  58.                         ? _T("AsyncCancelAll") : _T("")))
  59.                 );
  60.     }
  61.     else
  62.         TRACE(_T("CheckCaps - Async not supported\n"));
  63. }
  64.  
  65.  
  66.  
  67.  
  68. //-----------------------------------------------------------------------------
  69. // Name: DPUtil_FreeDirectPlay()
  70. // Desc: Wrapper for DirectPlay Close API
  71. //-----------------------------------------------------------------------------
  72. HRESULT DPUtil_FreeDirectPlay()
  73. {
  74.     if( NULL == g_pDP )
  75.         return E_FAIL;
  76.  
  77.     return g_pDP->Close();
  78. }
  79.  
  80.  
  81.  
  82.  
  83. //-----------------------------------------------------------------------------
  84. // Name: DPUtil_InitDirectPlay()
  85. // Desc: Wrapper for DirectPlay Create API. Retrieves a DirectPlay4/4A
  86. //       interface based on the UNICODE flag
  87. //-----------------------------------------------------------------------------
  88. HRESULT DPUtil_InitDirectPlay( VOID* pCon )
  89. {
  90.     HRESULT hr;
  91.  
  92.     // Create a DirectPlay4(A) interface
  93. #ifdef UNICODE
  94.     hr = CoCreateInstance( CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER,
  95.                            IID_IDirectPlay4, (VOID**)&g_pDP );
  96. #else
  97.     hr = CoCreateInstance( CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER,
  98.                            IID_IDirectPlay4A, (VOID**)&g_pDP );
  99. #endif
  100.     if( FAILED(hr) )
  101.         return hr;
  102.  
  103.     // Initialize w/address
  104.     if( pCon )
  105.     {
  106.         hr = g_pDP->InitializeConnection( pCon, 0 );
  107.         if( FAILED(hr) )
  108.         {
  109.             g_pDP->Release();
  110.             g_pDP = NULL;
  111.             return hr;
  112.         }
  113.     }
  114.  
  115.     return S_OK;
  116. }
  117.  
  118.  
  119.  
  120.  
  121. //-----------------------------------------------------------------------------
  122. // Name: DPUtil_CreatePlayer()
  123. // Desc: Wrapper for DirectPlay CreatePlayer API. 
  124. //-----------------------------------------------------------------------------
  125. HRESULT DPUtil_CreatePlayer( DPID* ppidID, TCHAR* strPlayerName, HANDLE hEvent, 
  126.                              VOID* pData, DWORD dwDataSize )
  127. {
  128.     if( NULL == g_pDP )
  129.         return E_FAIL;
  130.  
  131.     DPNAME dpname;
  132.     ZeroMemory( &dpname, sizeof(DPNAME) );
  133.     dpname.dwSize = sizeof(DPNAME);
  134.  
  135. #ifdef UNICODE
  136.     dpname.lpszShortName  = strPlayerName;
  137. #else
  138.     dpname.lpszShortNameA = strPlayerName;
  139. #endif
  140.  
  141.     return g_pDP->CreatePlayer( ppidID, &dpname, hEvent, pData, dwDataSize, 0 );
  142. }
  143.  
  144.  
  145.  
  146.  
  147. //-----------------------------------------------------------------------------
  148. // Name: DPUtil_CreateSession()
  149. // Desc: Wrapper for DirectPlay CreateSession API.Uses the global application
  150. //       guid.
  151. //-----------------------------------------------------------------------------
  152. HRESULT DPUtil_CreateSession( TCHAR* strSessionName )
  153. {
  154.     if( NULL == g_pDP )
  155.         return DPERR_NOINTERFACE;
  156.  
  157.     DPSESSIONDESC2 dpDesc;
  158.     ZeroMemory( &dpDesc, sizeof(dpDesc) );
  159.     dpDesc.dwSize  = sizeof(dpDesc);
  160.     dpDesc.dwFlags = DPSESSION_MIGRATEHOST | DPSESSION_KEEPALIVE;
  161.     if( g_bUseProtocol )
  162.         dpDesc.dwFlags |= DPSESSION_DIRECTPLAYPROTOCOL;
  163.  
  164. #ifdef UNICODE
  165.     dpDesc.lpszSessionName  = strSessionName;
  166. #else
  167.     dpDesc.lpszSessionNameA = strSessionName;
  168. #endif
  169.  
  170.     // Set the application guid
  171.     dpDesc.guidApplication = g_AppGUID;
  172.  
  173.     HRESULT hr = g_pDP->Open( &dpDesc, DPOPEN_CREATE );
  174.  
  175.     // Check for Async message support
  176.     if( SUCCEEDED(hr) )
  177.         CheckCaps();
  178.  
  179.     return hr;
  180. }
  181.  
  182.  
  183.  
  184.  
  185. //-----------------------------------------------------------------------------
  186. // Name: DPUtil_DestroyPlayer()
  187. // Desc: Wrapper for DirectPlay DestroyPlayer API. 
  188. //-----------------------------------------------------------------------------
  189. HRESULT DPUtil_DestroyPlayer( DPID pid )
  190. {
  191.     if( NULL == g_pDP )
  192.         return E_FAIL;
  193.  
  194.     return g_pDP->DestroyPlayer( pid );
  195. }
  196.  
  197.  
  198.  
  199.  
  200. //-----------------------------------------------------------------------------
  201. // Name: DPUtil_EnumPlayers()
  202. // Desc: Wrapper for DirectPlay API EnumPlayers
  203. //-----------------------------------------------------------------------------
  204. HRESULT DPUtil_EnumPlayers( GUID* pSessionGuid,
  205.                           LPDPENUMPLAYERSCALLBACK2 pEnumCallback, 
  206.                           VOID* pContext, DWORD dwFlags )
  207. {
  208.     if( NULL == g_pDP )
  209.         return E_FAIL;
  210.  
  211.     return g_pDP->EnumPlayers( pSessionGuid, pEnumCallback, pContext, dwFlags );
  212. }
  213.  
  214.  
  215.  
  216.  
  217. //-----------------------------------------------------------------------------
  218. // Name: DPUtil_EnumSessions()
  219. // Desc: Wrapper for DirectPlay EnumSessions API.
  220. //-----------------------------------------------------------------------------
  221. HRESULT DPUtil_EnumSessions( DWORD dwTimeout,
  222.                            LPDPENUMSESSIONSCALLBACK2 pEnumCallback, 
  223.                            VOID* pContext, DWORD dwFlags )
  224. {
  225.     if( NULL == g_pDP )
  226.         return E_FAIL;
  227.  
  228.     DPSESSIONDESC2 dpDesc;
  229.     ZeroMemory( &dpDesc, sizeof(dpDesc) );
  230.     dpDesc.dwSize = sizeof(dpDesc);
  231.     dpDesc.guidApplication = g_AppGUID;
  232.  
  233.     return g_pDP->EnumSessions( &dpDesc, dwTimeout, pEnumCallback,
  234.                                 pContext, dwFlags );
  235. }
  236.  
  237.  
  238.  
  239.  
  240. //-----------------------------------------------------------------------------
  241. // Name: DPUtil_GetPlayerLocalData()
  242. // Desc: Wrapper for DirectPlay GetPlayerData API.
  243. //-----------------------------------------------------------------------------
  244. HRESULT DPUtil_GetPlayerLocalData( DPID pid, VOID* pData, DWORD* pdwDataSize )
  245. {
  246.     if( NULL == g_pDP )
  247.         return E_FAIL;
  248.  
  249.     HRESULT hr = g_pDP->GetPlayerData( pid, pData, pdwDataSize, DPGET_LOCAL );
  250.     if( FAILED(hr) )
  251.         TRACE( TEXT("Get Player local data failed for id %d\n"), pid );
  252.  
  253.     return hr;
  254. }
  255.  
  256.  
  257.  
  258.  
  259. //-----------------------------------------------------------------------------
  260. // Name: DPUtil_GetSessionDesc()
  261. // Desc: Wrapper for DirectPlay GetSessionDesc API. 
  262. //-----------------------------------------------------------------------------
  263. HRESULT DPUtil_GetSessionDesc()
  264. {
  265.     DWORD   dwSize;
  266.     HRESULT hr;
  267.  
  268.     // Free old session desc, if any
  269.     if( g_pdpsd )
  270.         free( g_pdpsd );
  271.     g_pdpsd = NULL;
  272.  
  273.     if( NULL == g_pDP )
  274.         return E_FAIL;
  275.  
  276.     // First get the size for the session desc
  277.     hr = g_pDP->GetSessionDesc( NULL, &dwSize );
  278.     if( DPERR_BUFFERTOOSMALL == hr )
  279.     {
  280.         // Allocate memory for it
  281.         g_pdpsd = (DPSESSIONDESC2*)malloc( dwSize );
  282.         if( NULL == g_pdpsd )
  283.             return E_OUTOFMEMORY;
  284.  
  285.         // Now get the session desc
  286.         hr = g_pDP->GetSessionDesc( g_pdpsd, &dwSize );
  287.     }
  288.  
  289.     return hr;
  290. }
  291.  
  292.  
  293.  
  294.  
  295. //-----------------------------------------------------------------------------
  296. // Name: DPUtil_IsDPlayInitialized()
  297. // Desc: Returns TRUE if a DirectPlay interface exists, otherwise FALSE.
  298. //-----------------------------------------------------------------------------
  299. BOOL DPUtil_IsDPlayInitialized()
  300. {
  301.     return( g_pDP ? TRUE : FALSE );
  302. }
  303.  
  304.  
  305.  
  306.  
  307. //-----------------------------------------------------------------------------
  308. // Name: DPUtil_OpenSession()
  309. // Desc: Wrapper for DirectPlay OpenSession API. 
  310. //-----------------------------------------------------------------------------
  311. HRESULT DPUtil_OpenSession( GUID* pSessionGUID )
  312. {
  313.     if( NULL == g_pDP)
  314.         return DPERR_NOINTERFACE;
  315.  
  316.     DPSESSIONDESC2 dpDesc;
  317.     ZeroMemory( &dpDesc, sizeof(dpDesc) );
  318.     dpDesc.dwSize = sizeof(dpDesc);
  319.     if( g_bUseProtocol )
  320.         dpDesc.dwFlags = DPSESSION_DIRECTPLAYPROTOCOL;
  321.  
  322.     // Set the session guid
  323.     if( pSessionGUID )
  324.         dpDesc.guidInstance = (*pSessionGUID);
  325.     // Set the application guid
  326.     dpDesc.guidApplication = g_AppGUID;
  327.  
  328.     // Open it
  329.     HRESULT hr = g_pDP->Open( &dpDesc, DPOPEN_JOIN );
  330.  
  331.     // Check for Async message support
  332.     if( SUCCEEDED(hr) )
  333.         CheckCaps();
  334.  
  335.     return hr;
  336. }
  337.  
  338.  
  339.  
  340.  
  341. //-----------------------------------------------------------------------------
  342. // Name: DPUtil_Receive()
  343. // Desc: Wrapper for DirectPlay Receive API
  344. //-----------------------------------------------------------------------------
  345. HRESULT DPUtil_Receive( DPID* pidFrom, DPID* pidTo, DWORD dwFlags, VOID* pData,
  346.                       DWORD* pdwDataSize )
  347. {
  348.     if( NULL == g_pDP )
  349.         return E_FAIL;
  350.  
  351.     return g_pDP->Receive( pidFrom, pidTo, dwFlags, pData, pdwDataSize );
  352. }
  353.  
  354.  
  355.  
  356.  
  357. //-----------------------------------------------------------------------------
  358. // Name: DPUtil_Release()
  359. // Desc: Wrapper for DirectPlay Release API.
  360. //-----------------------------------------------------------------------------
  361. HRESULT DPUtil_Release()
  362. {
  363.     if( NULL == g_pDP )
  364.         return E_FAIL;
  365.  
  366.     // Free session desc, if any
  367.     if( g_pdpsd ) 
  368.         free( g_pdpsd );
  369.     g_pdpsd = NULL;
  370.  
  371.     // Free connection settings structure, if any (lobby stuff)
  372.     if( g_pDPLConnection )
  373.         delete[] g_pDPLConnection;
  374.     g_pDPLConnection = NULL;
  375.  
  376.     // Release dplay
  377.     HRESULT hr = g_pDP->Release();
  378.     g_pDP = NULL;
  379.  
  380.     return hr;
  381. }
  382.  
  383.  
  384.  
  385.  
  386. //-----------------------------------------------------------------------------
  387. // Name: DPUtil_Send()
  388. // Desc: Wrapper for DirectPlay Send[Ex] API.
  389. //-----------------------------------------------------------------------------
  390. HRESULT DPUtil_Send( DPID idFrom, DPID idTo, DWORD dwFlags, VOID* pData,
  391.                    DWORD dwDataSize )
  392. {
  393.     if( NULL == g_pDP )
  394.         return DPERR_NOINTERFACE;
  395.  
  396.     if (dwFlags & DPSEND_ASYNC)
  397.         // We don't specify a priority or timeout.  Would have to check
  398.         // GetCaps() first to see if they were supported
  399.         return g_pDP->SendEx( idFrom, idTo, dwFlags, pData, dwDataSize,
  400.                               0, 0, NULL, NULL );
  401.     else
  402.         return g_pDP->Send( idFrom, idTo, dwFlags, pData, dwDataSize );
  403. }
  404.  
  405.  
  406.  
  407.  
  408. //-----------------------------------------------------------------------------
  409. // Name: DPUtil_SetPlayerLocalData()
  410. // Desc: Wrapper for DirectPlay SetPlayerData API
  411. //-----------------------------------------------------------------------------
  412. HRESULT DPUtil_SetPlayerLocalData( DPID pid, VOID* pData, DWORD dwSize )
  413. {
  414.     if( NULL == g_pDP )
  415.         return E_FAIL;
  416.  
  417.     HRESULT hr = g_pDP->SetPlayerData( pid, pData, dwSize, DPSET_LOCAL );
  418.     if( FAILED(hr) )
  419.         TRACE( TEXT("Set Player local data failed for id %d\n"), pid );
  420.     
  421.     return hr;
  422. }
  423.  
  424.  
  425.  
  426.